﻿using System;
using System.Data.Common;
using System.Data.Entity.Infrastructure;
using WeBlog.Services;

namespace WeBlog.DataContext
{
    public class WeBlogConnectionFactory : IDbConnectionFactory
    {
        public DbConnection CreateConnection(string nameOrConnectionString)
        {
            var configuration = SiteManager.CurrentConfiguration;

            if (configuration.ProviderName.Equals("System.Data.SqlClient", StringComparison.InvariantCultureIgnoreCase))
            {
                var sqlFactory = new SqlConnectionFactory();
                return sqlFactory.CreateConnection(configuration.ConnectionString);
            }

            var sqlCeFactory = new SqlCeConnectionFactory(configuration.ProviderName);
            return sqlCeFactory.CreateConnection(configuration.ConnectionString);
        }
    }    
}
